home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 January: Mac OS SDK / Dev.CD Jan 99 SDK1.toast / Development Kits / Installer SDK 1.2 / Upgrader 1.2.1 & Engines / Upgrader 1.2.1 / Plug-in Examples / Common Files / Editor Utilities / EditorUtilities.cp < prev    next >
Encoding:
Text File  |  1998-08-25  |  22.6 KB  |  928 lines  |  [TEXT/CWIE]

  1.  
  2. #include <Gestalt.h>
  3. #include <LowMem.h>
  4. #include <Resources.h>
  5. #include <SCSI.h>
  6. #include <Script.h>
  7. #include <Start.h>
  8. #include <ToolUtils.h>
  9.  
  10. #include "EditorUtilities.h"
  11.  
  12.  
  13. /////////////////////////////////////////////////////////////////////////////////////////////////////
  14. //
  15. // Global Routines
  16. //
  17. /////////////////////////////////////////////////////////////////////////////////////////////////////
  18.  
  19. /*—————————————————————————————————————————————— PStrCmp
  20. ————————————————————————————————————————————————————————————————————————————*/
  21. /*———— 0 if equal, <0 if p2<p1, >0 if p2>p1  ————*/
  22.  
  23.  
  24.  
  25. SInt16    PStrCmp( ConstStr255Param p2, ConstStr255Param p1 )
  26. {
  27.     SInt16    i;
  28.     SInt16    len;
  29.     
  30.     if( (unsigned char)*p2 == (unsigned char)*p1 ) {
  31.         len = (unsigned char)*p2;
  32.         for( i=1; *p2 == *p1; p2++, p1++, i++ )
  33.             if( i > len )
  34.                 return 0;
  35.  
  36.         return (unsigned char)*p2 - (unsigned char)*p1;
  37.     }
  38.     else 
  39.         return (unsigned char)*p2 - (unsigned char)*p1;
  40.     
  41. }
  42.  
  43.  
  44. /*—————————————————————————————————————————————— PStrConcat
  45. ————————————————————————————————————————————————————————————————————————————*/
  46. /*———— dst = dst + src  ————*/
  47.  
  48. void PStrConcat( unsigned char* dst, const unsigned char* src )
  49. {
  50.     SInt16             srcLen;
  51.     SInt16             dstLen;
  52.     unsigned char*    ptr;
  53.     
  54.     srcLen = (unsigned char)*src;
  55.     dstLen = (unsigned char)*dst;
  56.     
  57.     if( dstLen + srcLen > 255 )
  58.         srcLen = 255 - dstLen;
  59.         
  60.     ptr = dst + dstLen;
  61.     
  62.     *dst = dstLen + srcLen;
  63.     while (--srcLen>=0) *++ptr = *++src;
  64.     
  65. }
  66.  
  67. /*—————————————————————————————————————————————— pstrcpy
  68. ————————————————————————————————————————————————————————————————————————————*/
  69. /*———— copies a pascal string from p1 to p2 ————*/
  70. void    PStrCopy( Str255 p2, ConstStr255Param p1 )
  71. {
  72.     short len;
  73.     
  74.     *p2 = *p1++;
  75.     len = (unsigned char)*p2++;
  76.     while (--len>=0) *p2++=*p1++;
  77.     
  78. }
  79.  
  80.  
  81.  
  82. void GetIndStringWithParam( Str255                 theString,
  83.                             SInt16                 strListID,
  84.                             SInt16                 index,
  85.                             ConstStr255Param    pParamText0,
  86.                             ConstStr255Param    pParamText1,
  87.                             ConstStr255Param    pParamText2,
  88.                             ConstStr255Param    pParamText3 )
  89. {
  90.  
  91.     Str255    tempStr255;
  92.  
  93.     // -- Get the string from our resource fork
  94.     GetIndString( tempStr255, strListID, index );
  95.     
  96.     // -- Setup base string handle
  97.     Handle    baseStrHandle = NewHandle(tempStr255[0]);
  98.     if( baseStrHandle )
  99.     {
  100.         BlockMove( &tempStr255[1], *baseStrHandle, tempStr255[0] );
  101.         
  102.         // -- Substitute ^0
  103.         if( pParamText0[0] > 0 )
  104.         {
  105.             Handle    subStrHandle = NewHandle(pParamText0[0]);
  106.             if( baseStrHandle )
  107.             {
  108.                 BlockMove( &pParamText0[1], *subStrHandle, pParamText0[0]);
  109.                 ReplaceText( baseStrHandle, subStrHandle, "\p^0" );
  110.                 DisposeHandle( subStrHandle );
  111.             }
  112.         
  113.         }
  114.         
  115.         // -- Substitute ^1
  116.         if( pParamText1[0] > 0 )
  117.         {
  118.             Handle    subStrHandle = NewHandle(pParamText1[0]);
  119.             if( baseStrHandle )
  120.             {
  121.                 BlockMove( &pParamText1[1], *subStrHandle, pParamText1[0]);
  122.                 ReplaceText( baseStrHandle, subStrHandle, "\p^1" );
  123.                 DisposeHandle( subStrHandle );
  124.             }
  125.         
  126.         }
  127.         
  128.         // -- Substitute ^2
  129.         if( pParamText2[0] > 0 )
  130.         {
  131.             Handle    subStrHandle = NewHandle(pParamText2[0]);
  132.             if( baseStrHandle )
  133.             {
  134.                 BlockMove( &pParamText2[1], *subStrHandle, pParamText2[0]);
  135.                 ReplaceText( baseStrHandle, subStrHandle, "\p^2" );
  136.                 DisposeHandle( subStrHandle );
  137.             }
  138.         
  139.         }
  140.         
  141.         // -- Substitute ^3
  142.         if( pParamText3[0] > 0 )
  143.         {
  144.             Handle    subStrHandle = NewHandle(pParamText3[0]);
  145.             if( baseStrHandle )
  146.             {
  147.                 BlockMove( &pParamText3[1], *subStrHandle, pParamText3[0]);
  148.                 ReplaceText( baseStrHandle, subStrHandle, "\p^3" );
  149.                 DisposeHandle( subStrHandle );
  150.             }
  151.         
  152.         }
  153.         
  154.         // Set the return string
  155.         SInt32 baseStringLen = GetHandleSize( baseStrHandle );
  156.         BlockMove( *baseStrHandle, &theString[1], baseStringLen );
  157.         
  158.         if( baseStringLen > 255 )
  159.             theString[0] = 255;
  160.         else
  161.             theString[0] = baseStringLen;
  162.  
  163.  
  164.         DisposeHandle( baseStrHandle );
  165.     }
  166.     else
  167.     {
  168.         theString[0] = 0;
  169.     }
  170.         
  171. }
  172.  
  173. SInt16 GetUniqueIDForResType( SInt16 inFileRefNum, ResType theResType )
  174. {
  175.     const    SInt16    kLowerLimit = 10000;
  176.     const    SInt16    kUpperLimit = 20000;
  177.  
  178.     SInt16 savedResFile = CurResFile();
  179.     UseResFile( inFileRefNum ) ;
  180.         
  181.     UInt16    hangPrevention = kLowerLimit;
  182.     SInt16    theUniqueID    = Unique1ID( theResType );
  183.     while( (theUniqueID < kLowerLimit || theUniqueID > kUpperLimit) && hangPrevention < kUpperLimit )
  184.     {
  185.         theUniqueID    = Unique1ID( theResType );
  186.         hangPrevention++;
  187.     }
  188.     
  189.     if( hangPrevention > kUpperLimit )
  190.         theUniqueID    = Unique1ID( theResType );
  191.  
  192.     UseResFile( savedResFile ) ;
  193.         
  194.     return theUniqueID;
  195. }
  196.  
  197.  
  198. void ResetResListResource( SInt16 inFileRefNum, SInt16 inResListRsrcID )
  199. {
  200.  
  201.     SInt16 savedResFile = CurResFile();
  202.     UseResFile( inFileRefNum ) ;
  203.         
  204.     Handle    theResListHandle = Get1Resource( 'RES#', inResListRsrcID );
  205.     
  206.     if( theResListHandle == NULL )
  207.     {
  208.         AddResource( NewHandle(2), 'RES#', inResListRsrcID, "\p" );
  209.         FailResError();
  210.         theResListHandle = Get1Resource( 'RES#', inResListRsrcID );
  211.         FailNil( theResListHandle );
  212.     }
  213.     else
  214.     {
  215.         SetHandleSize( theResListHandle, 2 );
  216.         FailMemError();
  217.     }
  218.  
  219.     SInt16    tempShort = 0;
  220.     BlockMove( &tempShort, *theResListHandle, 2 );
  221.     ChangedResource( theResListHandle );
  222.  
  223.     UseResFile( savedResFile ) ;
  224. }
  225.  
  226.  
  227. void AppendToResListResource( SInt16 inFileRefNum, SInt16 inResListRsrcID, ResType inRsrcType, SInt16 inRsrcID )
  228. {
  229.     if( inRsrcID != 0 )        // Don't add if rsrc ID is 0
  230.     {
  231.         SInt16 savedResFile = CurResFile();
  232.         UseResFile( inFileRefNum ) ;
  233.             
  234.         Handle    theResListHandle = Get1Resource( 'RES#', inResListRsrcID );
  235.         
  236.         if( theResListHandle != NULL )
  237.         {
  238.             PtrAndHand( &inRsrcType, theResListHandle, sizeof(ResType) );
  239.             PtrAndHand( &inRsrcID, theResListHandle, 2 );
  240.  
  241.             SInt16    tempShort = 0;
  242.             BlockMove( *theResListHandle,  &tempShort, 2 );
  243.             tempShort++;
  244.             BlockMove( &tempShort, *theResListHandle, 2 );
  245.             
  246.             ChangedResource( theResListHandle );
  247.         }
  248.  
  249.         UseResFile( savedResFile ) ;
  250.     }
  251. }
  252.  
  253.  
  254. void DeleteResource( SInt16 inFileRefNum, ResType inRsrcType, SInt16 inRsrcID )
  255. {
  256.     if( inRsrcID != 0 )        // Don't try deleting if rsrc ID is 0
  257.     {
  258.         SInt16 savedResFile = CurResFile();
  259.         UseResFile( inFileRefNum ) ;
  260.             
  261.         Handle    theRsrcHandle = Get1Resource( inRsrcType, inRsrcID );
  262.         
  263.         if( theRsrcHandle != NULL )
  264.         {
  265.             RemoveResource( theRsrcHandle );
  266.             DisposeHandle( theRsrcHandle );
  267.             
  268.         }
  269.  
  270.         UseResFile( savedResFile ) ;
  271.     }
  272. }
  273.  
  274.  
  275. void WriteStringResource( SInt16 inRsrcID, Str255 inString )
  276. {
  277.     StringHandle theStrHandle = GetString( inRsrcID );
  278.     
  279.     if( theStrHandle == NULL )
  280.     {
  281.         AddResource( (Handle)NewString( inString ), 'STR ', inRsrcID, "\p" );
  282.         FailResError();
  283.     }
  284.     else
  285.     {
  286.         SetHandleSize( (Handle)theStrHandle, inString[0] + 1 );
  287.         FailMemError();
  288.         BlockMove( &inString[0], *theStrHandle, inString[0] + 1 );
  289.         ChangedResource( (Handle)theStrHandle );
  290.     }
  291.  
  292. }
  293.  
  294. void WriteStringListResourceIndex( SInt16 inFileRefNum, Str255 inString, SInt16 inRsrcID, SInt16 inIndex )
  295. {
  296.     SInt16 savedResFile = CurResFile();
  297.     UseResFile( inFileRefNum );
  298.     
  299.     UInt16    totalNumOfItems        = inIndex;
  300.  
  301.     Handle    theOrgStrListHandle = Get1Resource( 'STR#', inRsrcID );
  302.     if( theOrgStrListHandle &&  **((UInt16**)theOrgStrListHandle) > totalNumOfItems )
  303.         totalNumOfItems = **((UInt16**)theOrgStrListHandle);
  304.     
  305.     TRsrcHandle* theRsrcStream = new TRsrcHandle;
  306.     SignalIf_(theRsrcStream == nil);
  307.     
  308.     theRsrcStream->AppendShort( totalNumOfItems );
  309.     
  310.     Str255    tempStr;
  311.     for( UInt16 i = 1; i <= totalNumOfItems; i++ )
  312.     {
  313.         GetIndString( tempStr, inRsrcID, i );
  314.         
  315.         if( i == inIndex )
  316.             theRsrcStream->AppendStr255WithoutPadByte( inString );
  317.         else
  318.             theRsrcStream->AppendStr255WithoutPadByte( tempStr );
  319.     }
  320.  
  321.     theRsrcStream->Write( inFileRefNum, 'STR#', inRsrcID );
  322.  
  323.     delete theRsrcStream;
  324.  
  325.     UseResFile( savedResFile );
  326.  
  327. }
  328.  
  329.  
  330. OSErr GetFileNameFromFileRefID( SInt16 inFileRefID, Str255 outFileName )
  331. {
  332.     const    ResType        kFileRefRsrcType    = 'flrf';
  333.     
  334.     OSErr                theErr                = noErr;
  335.  
  336.     try
  337.     {
  338.         outFileName[0] = 0;
  339.     
  340.         // Get File Ref Resource
  341.         Handle theFileRefRsrcHandle = GetResource( kFileRefRsrcType, inFileRefID );
  342.         if( theFileRefRsrcHandle )
  343.         {
  344.         
  345.             // Create TRsrcHandleObj from file ref resource.
  346.             TRsrcHandle*    theFileRef         = new TRsrcHandle( theFileRefRsrcHandle );
  347.             FailNil( theFileRef );
  348.         
  349.             // Get fields from resource
  350.             if( theFileRef->ReadShort() == 0 )    // Skip format number.
  351.             {        
  352.                 theFileRef->ReadShort();        // Skip unused flags.
  353.                 theFileRef->ReadShort();        // Skip media type.
  354.                 theFileRef->ReadLong();            // Skip file type.
  355.                 theFileRef->ReadLong();            // Skip file creator.
  356.                 theFileRef->ReadLong();            // Skip file creation date.
  357.                 theFileRef->ReadPStrIntoStr255( outFileName );    // using outFileName as temp var to read path
  358.                 theFileRef->ReadPStrIntoStr255( outFileName );    // read file name into var to return
  359.             }
  360.             else
  361.                 theErr = fnfErr;
  362.                 
  363.             delete theFileRef;
  364.         }
  365.         else
  366.             theErr = fnfErr;
  367.         
  368.     }
  369.     catch( OSErr catchErr )
  370.     {
  371.         theErr = catchErr;
  372.     }
  373.     
  374.     return theErr;
  375. }
  376.  
  377.  
  378. EKeyStatus    PrintingCharWithReturnField(const EventRecord    &inKeyEvent)
  379. {
  380.  
  381.     EKeyStatus    theKeyStatus = keyStatus_PassUp;
  382.     Char16        theKey = inKeyEvent.message;
  383.     Char16        theChar = theKey & charCodeMask;
  384.     
  385.     if (UKeyFilters::IsTEDeleteKey(theKey)) {
  386.         theKeyStatus = keyStatus_TEDelete;
  387.     } else if (UKeyFilters::IsTECursorKey(theKey)) {
  388.         theKeyStatus = keyStatus_TECursor;
  389.     } else if (UKeyFilters::IsExtraEditKey(theKey)) {
  390.         theKeyStatus = keyStatus_ExtraEdit;
  391.     } else if (UKeyFilters::IsPrintingChar(theChar)) {
  392.         theKeyStatus = keyStatus_Input;
  393.     } else if (theChar == 0x0D )
  394.         theKeyStatus = keyStatus_Input;
  395.  
  396.     return theKeyStatus;
  397.  
  398. }
  399.  
  400.  
  401. EKeyStatus    SignedIntegerField(const EventRecord    &inKeyEvent)
  402. {
  403.     EKeyStatus    theKeyStatus = keyStatus_PassUp;
  404.     Char16        theKey = inKeyEvent.message;
  405.     Char16        theChar = theKey & charCodeMask;
  406.     
  407.     if (UKeyFilters::IsTEDeleteKey(theKey)) {
  408.         theKeyStatus = keyStatus_TEDelete;
  409.     } else if (UKeyFilters::IsTECursorKey(theKey)) {
  410.         theKeyStatus = keyStatus_TECursor;
  411.     } else if (UKeyFilters::IsExtraEditKey(theKey)) {
  412.         theKeyStatus = keyStatus_ExtraEdit;
  413.     } else if (UKeyFilters::IsPrintingChar(theChar)) {
  414.         if (UKeyFilters::IsNumberChar(theChar)) {
  415.             theKeyStatus = keyStatus_Input;
  416.         } else if (theChar == '-' ) {
  417.             theKeyStatus = keyStatus_Input;
  418.         } else {
  419.             theKeyStatus = keyStatus_Reject;
  420.         }
  421.     }
  422.     
  423.     return theKeyStatus;
  424. }
  425.  
  426.  
  427.  
  428. /////////////////////////////////////////////////////////////////////////////////////////////////////
  429. //
  430. // class TRsrcHandle - class for reading and writing complex structured resources
  431. //
  432. /////////////////////////////////////////////////////////////////////////////////////////////////////
  433.  
  434. // ---------------------- •    TRsrcHandle -----------------------------------
  435.     TRsrcHandle::TRsrcHandle()
  436.     {
  437.         fRsrcHandle = NewHandle( 0 );
  438.         FailMemError();
  439.         SetPos( 1 );
  440.     }
  441.  
  442. // ---------------------- •    TRsrcHandle -----------------------------------
  443.     TRsrcHandle::TRsrcHandle( Handle    pInitilizedHandle )
  444.     {
  445.         if( pInitilizedHandle == NULL )
  446.         {
  447.             fRsrcHandle = NewHandle( 0 );
  448.             FailMemError();
  449.         }
  450.         else {
  451.             fRsrcHandle = pInitilizedHandle;
  452.             FailErr( HandToHand( &fRsrcHandle ) );
  453.         }
  454.         SetPos( 1 );
  455.     }
  456.  
  457. // ---------------------- •    TRsrcHandle -----------------------------------
  458.     TRsrcHandle::TRsrcHandle( Ptr    pInitilizedPtr )
  459.     {
  460.         if( pInitilizedPtr == NULL )
  461.         {
  462.             fRsrcHandle = NewHandle( GetPtrSize(pInitilizedPtr) );
  463.             FailMemError();
  464.             
  465.             PtrToHand( pInitilizedPtr, &fRsrcHandle, GetPtrSize(pInitilizedPtr) );
  466.         }
  467.         SetPos( 1 );
  468.     }
  469.  
  470.  
  471. // ---------------------- •    ~TRsrcHandle -----------------------------------
  472.     TRsrcHandle::~TRsrcHandle()
  473.     {
  474.         DetachResource( fRsrcHandle );    // In case it's a rsrc handle
  475.         DisposeHandle( fRsrcHandle );
  476.     }
  477.  
  478.  
  479. // ---------------------- •    AppendLong -----------------------------------
  480.     void TRsrcHandle::AppendLong(    SInt32    pTheLong )
  481.     {
  482.         Handle    longHandle = NewHandle( sizeof( SInt32 ) );
  483.         FailMemError();
  484.         (**(SInt32**)longHandle) = pTheLong;
  485.         
  486.         HLock(longHandle);
  487.         FailErr( HandAndHand( longHandle, fRsrcHandle ) );
  488.         DisposeHandle( longHandle );
  489.     }
  490.  
  491.  
  492. // ---------------------- •    AppendShort -----------------------------------
  493.     void TRsrcHandle::AppendShort(     SInt16    pTheShort )
  494.     {
  495.         Handle    shortHandle = NewHandle( 2 );
  496.         FailMemError();
  497.         (**(SInt16**)shortHandle) = pTheShort;
  498.         
  499.         HLock(shortHandle);
  500.         FailErr( HandAndHand( shortHandle, fRsrcHandle ) );
  501.         DisposeHandle( shortHandle );
  502.     }
  503.  
  504.  
  505. // ---------------------- •    AppendStr255WithoutPadByte -----------------------------------
  506.     void TRsrcHandle::AppendStr255WithoutPadByte( Str255    pTheStr255 )
  507.     {
  508.         char    *curCharPtr;
  509.         SInt16    lenToCreate    = pTheStr255[0] + 1;
  510.  
  511.         Handle    tempHandle;
  512.         
  513.         tempHandle = NewHandle( lenToCreate );
  514.         FailMemError();
  515.         HLock(tempHandle);
  516.         curCharPtr = *tempHandle;
  517.  
  518.         // copy the Str255 into the handle    
  519.         for(SInt16 i=0; i<=pTheStr255[0]; i++, curCharPtr++)
  520.             (*curCharPtr) = pTheStr255[i];
  521.         
  522.         FailErr( HandAndHand( tempHandle, fRsrcHandle ) );
  523.         DisposeHandle( tempHandle );
  524.     }
  525.  
  526. // ---------------------- •    AppendStr255 -----------------------------------
  527.     void TRsrcHandle::AppendStr255( Str255    pTheStr255 )
  528.     {
  529.         char    *curCharPtr;
  530.         SInt16    lenToCreate    = pTheStr255[0] + 1;
  531.         Boolean    pad1Byte    = false;
  532.  
  533.         Handle    tempHandle;
  534.         
  535.         // If the length we’re creating is odd, we bump the length by 1 to do word aligned strings
  536.         if( ( lenToCreate % 2 ) != 0 ) {
  537.             lenToCreate++;
  538.             pad1Byte = true;
  539.         }
  540.             
  541.         tempHandle = NewHandle( lenToCreate );
  542.         FailMemError();
  543.         HLock(tempHandle);
  544.         curCharPtr = *tempHandle;
  545.  
  546.         // copy the Str255 into the handle    
  547.         for(SInt16 i=0; i<=pTheStr255[0]; i++, curCharPtr++)
  548.             (*curCharPtr) = pTheStr255[i];
  549.         
  550.         // fill out the pad byte if necessary
  551.         if( pad1Byte )
  552.             (*curCharPtr) = '\0';
  553.  
  554.         FailErr( HandAndHand( tempHandle, fRsrcHandle ) );
  555.         DisposeHandle( tempHandle );
  556.     }
  557.  
  558. // ---------------------- •    AppendStr255AsData -----------------------------------
  559.     void TRsrcHandle::AppendStr255AsData( Str255    pTheStr255 )
  560.     {
  561.     // -- Adds the Str255 without the length byte.  Good for adding Str255 variables to
  562.     //    windows or data forks.
  563.         char    *curCharPtr;
  564.         Handle    tempHandle = NewHandle( pTheStr255[0] );
  565.         FailMemError();
  566.         HLock(tempHandle);
  567.         curCharPtr = *tempHandle;
  568.         for(SInt16 i=1; i<=pTheStr255[0]; i++, curCharPtr++)
  569.             (*curCharPtr) = pTheStr255[i];
  570.         FailErr( HandAndHand( tempHandle, fRsrcHandle ) );
  571.         DisposeHandle( tempHandle );
  572.     }
  573.  
  574.  
  575. // ---------------------- •    AppendHandle -----------------------------------
  576.     void TRsrcHandle::AppendHandle( Handle    pTheHandle )
  577.     {
  578.         SignedByte lastHandleState = HGetState(pTheHandle);
  579.         HLock(pTheHandle);
  580.         FailErr( HandAndHand( pTheHandle, fRsrcHandle ) );
  581.         HSetState(pTheHandle, lastHandleState);
  582.     }
  583. /*
  584. // ---------------------- •    AppendStr255FromHandle -----------------------------------
  585.     void TRsrcHandle::AppendStr255FromHandle( Handle    pTheHandle )
  586.     {        
  587.         AppendStr255( BKMakeStr255FromHandle( pTheHandle ) );
  588.     }
  589. */
  590.     
  591. // ---------------------- •    Write -----------------------------------
  592.     void TRsrcHandle::Write(    SInt16                pFileRefNum,
  593.                                 OSType                pRsrcType,
  594.                                 SInt16                pRsrcID )
  595.     {    
  596.         Handle    tempHandle;
  597.         SInt16    savedResFile = CurResFile();
  598.         UseResFile( pFileRefNum );
  599.         
  600.         tempHandle = Get1Resource( pRsrcType, pRsrcID );
  601.         if( tempHandle ) {
  602.             RemoveResource( tempHandle );
  603.             DisposeHandle( tempHandle );
  604.         }
  605.         FailResError();
  606.         AddResource( fRsrcHandle, pRsrcType, pRsrcID, "\p" );
  607.         FailResError();
  608.         
  609.         UpdateResFile( pFileRefNum );
  610.  
  611.         DetachResource( fRsrcHandle );
  612.         
  613.         UseResFile( savedResFile );
  614.     }
  615.  
  616. // ---------------------- •    ReadByteIntoShort -----------------------------------
  617.     SInt16 TRsrcHandle::ReadByteIntoShort()
  618.     {
  619.         UInt16    tempShort = 0;
  620.     
  621.         ReadChunk( (Ptr)&tempShort, 1 );
  622.         tempShort >>= 8;
  623.         
  624.         return (SInt16)tempShort;
  625.     }
  626.  
  627.     
  628. // ---------------------- •    ReadLong -----------------------------------
  629.     SInt32 TRsrcHandle::ReadLong()
  630.     {
  631.         SInt32    tempLong = 0;
  632.     
  633.         ReadChunk( (Ptr)&tempLong, sizeof( SInt32 ) );
  634.         
  635.         return tempLong;
  636.     }
  637.     
  638. // ---------------------- •    ReadShort -----------------------------------
  639.     SInt16 TRsrcHandle::ReadShort()
  640.     {
  641.         SInt16    tempShort = 0;
  642.     
  643.         ReadChunk( (Ptr)&tempShort, 2 );
  644.         
  645.         return tempShort;
  646.     }
  647.  
  648.  
  649. // ---------------------- •    ReadPStrIntoHandle -----------------------------------
  650.     Handle TRsrcHandle::ReadPStrIntoHandle()
  651.     {    
  652.         SInt16    stringLen = ReadByteIntoShort() ;
  653.         Handle tempHandle = NewHandle( stringLen);
  654.         if( tempHandle != NULL )
  655.             ReadChunk( *tempHandle, stringLen );
  656.             
  657.         // If the length we’re reading is odd, we need to read one more byte.
  658.         if( ( stringLen % 2 ) == 0 )
  659.             ReadByteIntoShort();
  660.             
  661.         return tempHandle;
  662.     }
  663.  
  664.  
  665. // ---------------------- •    ReadPStrIntoStr255 -----------------------------------
  666.     void TRsrcHandle::ReadPStrIntoStr255( Str255    outStr255 )
  667.     {    
  668.         SInt16    stringLen = ReadByteIntoShort();
  669.         
  670.         ReadChunk( (Ptr)&(outStr255[1]), stringLen );
  671.             
  672.         // If the length we’re reading is odd, we need to read one more byte.
  673.         if( ( stringLen % 2 ) == 0 )
  674.             ReadByteIntoShort();
  675.         
  676.         outStr255[0] = stringLen;
  677.     }
  678.  
  679. // ---------------------- •    ReadChunk -----------------------------------
  680.     void TRsrcHandle::ReadChunk(Ptr        pDest,
  681.                                 SInt32     pSize )
  682.     {
  683.     // ReadChunk from the Installer.
  684.     
  685.         Ptr theSrc = StripAddress( *fRsrcHandle ) + fCurPos - 1;
  686.         BlockMove( theSrc, pDest, pSize );
  687.         fCurPos += pSize;
  688.     }
  689.  
  690.  
  691.  
  692.  
  693. /*
  694. void VersionNumToString( UInt32    inVersionNum, Str255 outFormattedStr )
  695. {
  696.     #define kMajorVersMask        0xFF000000
  697.     #define kMinorVersMask        0x00F00000
  698.     #define kBugFixVersMask        0x000F0000
  699.     #define kStageVersMask        0x0000FF00
  700.     #define kReleaseVersMask    0x000000FF
  701.     
  702.     #define kMajorVersShift        24
  703.     #define kMinorVersShift        20
  704.     #define kBugFixVersShift    16
  705.     #define kStageVersShift        8
  706.     #define kReleaseVersShift    0
  707.  
  708.     #define kDevel                0x20
  709.     #define kAlpha                0x40
  710.     #define kBeta                0x60
  711.     #define kFinal                0x80
  712.     
  713.     unsigned long    majorVersValue;
  714.     unsigned long    minorVersValue;
  715.     unsigned long    bufFixVersValue;
  716.     unsigned long    stageVersValue;
  717.     unsigned long    releaseVersValue;
  718.     Str255    tempVersStr;
  719.     
  720.     outFormattedStr[0] = 0;
  721.     
  722.     if( inVersionNum < 0x0000FFFF && inVersionNum > 0 ) {
  723.     
  724.         // —— Handle old style
  725.     
  726.         majorVersValue = inVersionNum / 100;
  727.         NumToString( majorVersValue, tempVersStr );
  728.         PStrConcat( outFormattedStr, tempVersStr );
  729.         
  730.         minorVersValue = inVersionNum / 10 - majorVersValue * 10;
  731.         NumToString( minorVersValue, tempVersStr );
  732.         PStrConcat( outFormattedStr, "\p." );
  733.         PStrConcat( outFormattedStr, tempVersStr );
  734.             
  735.         bufFixVersValue = inVersionNum - majorVersValue  * 100 - minorVersValue * 10;
  736.  
  737.         if( bufFixVersValue != 0 ) {
  738.             NumToString( bufFixVersValue, tempVersStr );
  739.             PStrConcat( outFormattedStr, "\p." );
  740.             PStrConcat( outFormattedStr, tempVersStr );
  741.         }
  742.     }
  743.     else {
  744.     
  745.         // —— Handle standard version number format with build stage and release
  746.  
  747.         majorVersValue = (inVersionNum & kMajorVersMask) >> kMajorVersShift;
  748.         NumToString( majorVersValue, tempVersStr );
  749.         PStrConcat( outFormattedStr, tempVersStr );
  750.         
  751.         minorVersValue = (inVersionNum & kMinorVersMask) >> kMinorVersShift;
  752.         NumToString( minorVersValue, tempVersStr );
  753.         PStrConcat( outFormattedStr, "\p." );
  754.         PStrConcat( outFormattedStr, tempVersStr );
  755.             
  756.         bufFixVersValue = (inVersionNum & kBugFixVersMask) >> kBugFixVersShift;
  757.         if( bufFixVersValue != 0 ) {
  758.             NumToString( bufFixVersValue, tempVersStr );
  759.             PStrConcat( outFormattedStr, "\p." );
  760.             PStrConcat( outFormattedStr, tempVersStr );
  761.         }
  762.     
  763.         stageVersValue = (inVersionNum & kStageVersMask) >> kStageVersShift;
  764.         releaseVersValue = (inVersionNum & kReleaseVersMask) >> kReleaseVersShift;
  765.     
  766.         if( releaseVersValue != 0 ) {
  767.         
  768.             switch( stageVersValue ) {
  769.                 case kDevel:
  770.                     PStrConcat( outFormattedStr, "\pd" );
  771.                     break;
  772.                 case kAlpha:
  773.                     PStrConcat( outFormattedStr, "\pa" );
  774.                     break;
  775.                 case kBeta:
  776.                     PStrConcat( outFormattedStr, "\pb" );
  777.                     break;
  778.                 case kFinal:
  779.                     PStrConcat( outFormattedStr, "\pf" );
  780.                     break;
  781.             }
  782.             
  783.             NumToString( releaseVersValue, tempVersStr );
  784.             PStrConcat( outFormattedStr, tempVersStr );
  785.         
  786.         }
  787.     }
  788.                 
  789. }
  790.  
  791.  
  792. void NumToCommaString(    SInt32       pLongNum,
  793.                          Str255      pOutputStr )
  794. {
  795.                                 
  796.     Str255    theCommaString;
  797.     
  798.     if( pLongNum == 0 )
  799.     {
  800.     
  801.         GetIndString( pOutputStr, kUtilityStringsSTRListRsrcID, kZeroStringIndex );
  802.     }
  803.     else
  804.     {
  805.         NumToString( pLongNum, pOutputStr );
  806.         
  807.         GetIndString( theCommaString, kUtilityStringsSTRListRsrcID, kCommaStringIndex );
  808.         
  809.         if( pOutputStr[0] > 6 )
  810.         {
  811.             BlockMove( (Ptr)pOutputStr + pOutputStr[0] - 5, (Ptr)pOutputStr + pOutputStr[0] - 5 + theCommaString[0], 6 );
  812.             BlockMove( (Ptr)&theCommaString[1], (Ptr)pOutputStr + pOutputStr[0] - 5, theCommaString[0] );
  813.             pOutputStr[0] += theCommaString[0];
  814.         }
  815.         
  816.         if( pOutputStr[0] > 3 )
  817.         {
  818.             BlockMove( (Ptr)pOutputStr + pOutputStr[0] - 2, (Ptr)pOutputStr + pOutputStr[0] - 2 + theCommaString[0], 3 );
  819.             BlockMove( (Ptr)&theCommaString[1], (Ptr)pOutputStr + pOutputStr[0] - 2, theCommaString[0] );
  820.             pOutputStr[0] += theCommaString[0];
  821.         }
  822.     }
  823.     
  824. }
  825. */
  826.  
  827.  
  828.  
  829. /////////////////////////////////////////////////////////////////////////////////////////////////////
  830. //
  831. // Class LShortComparator
  832. //
  833. //    Compares items as Short integer values
  834. //
  835. /////////////////////////////////////////////////////////////////////////////////////////////////////
  836.  
  837.  
  838. LShortComparator*    LShortComparator::sShortComparator;
  839.  
  840. Int32
  841. LShortComparator::Compare(
  842.     const void*        inItemOne,
  843.     const void*        inItemTwo,
  844.     Uint32            /* inSizeOne */,
  845.     Uint32            /* inSizeTwo */) const
  846. {
  847.     return ( (*(SInt16*) inItemOne) - (*(SInt16*) inItemTwo) );
  848. }
  849.  
  850.  
  851. Boolean
  852. LShortComparator::IsEqualTo(
  853.     const void*        inItemOne,
  854.     const void*        inItemTwo,
  855.     Uint32            /* inSizeOne */,
  856.     Uint32            /* inSizeTwo */) const
  857. {
  858.     return ( (*(SInt16*) inItemOne) == (*(SInt16*) inItemTwo) );
  859. }
  860.  
  861.  
  862. LShortComparator*
  863. LShortComparator::GetComparator()
  864. {
  865.     if (sShortComparator == nil) {
  866.         sShortComparator = new LShortComparator;
  867.     }
  868.     
  869.     return sShortComparator;
  870. }
  871.  
  872. /////////////////////////////////////////////////////////////////////////////////////////////////////
  873. //
  874. // Class LResTypeAndIDComparator
  875. //
  876. //    Compares items as Short integer values
  877. //
  878. /////////////////////////////////////////////////////////////////////////////////////////////////////
  879.  
  880.  
  881.  
  882. LResTypeAndIDComparator*    LResTypeAndIDComparator::sResTypeAndIDComparator;
  883.  
  884. Int32
  885. LResTypeAndIDComparator::Compare(
  886.     const void*        inItemOne,
  887.     const void*        inItemTwo,
  888.     Uint32            /* inSizeOne */,
  889.     Uint32            /* inSizeTwo */) const
  890. {
  891.  
  892.     Int32    resTypeResult = (((RsrcTypeAndID*)inItemOne)->fRsrcType) - (((RsrcTypeAndID*)inItemTwo)->fRsrcType);
  893.     Int32    IDResult = (((RsrcTypeAndID*)inItemOne)->fRsrcID) - (((RsrcTypeAndID*)inItemTwo)->fRsrcID);
  894.     
  895.     if( resTypeResult )
  896.         return resTypeResult;
  897.     else
  898.         return IDResult;
  899. }
  900.  
  901.  
  902. Boolean
  903. LResTypeAndIDComparator::IsEqualTo(
  904.     const void*        inItemOne,
  905.     const void*        inItemTwo,
  906.     Uint32            /* inSizeOne */,
  907.     Uint32            /* inSizeTwo */) const
  908. {
  909.     Boolean    resTypeResult = (((RsrcTypeAndID*)inItemOne)->fRsrcType) == (((RsrcTypeAndID*)inItemTwo)->fRsrcType);
  910.     Boolean    IDResult = (((RsrcTypeAndID*)inItemOne)->fRsrcID) == (((RsrcTypeAndID*)inItemTwo)->fRsrcID);
  911.  
  912.     return resTypeResult && IDResult;
  913. }
  914.  
  915.  
  916. LResTypeAndIDComparator*
  917. LResTypeAndIDComparator::GetComparator()
  918. {
  919.     if (sResTypeAndIDComparator == nil) {
  920.         sResTypeAndIDComparator = new LResTypeAndIDComparator;
  921.     }
  922.     
  923.     return sResTypeAndIDComparator;
  924.  
  925.  
  926. }
  927.  
  928.